<template>
  <el-form ref="formRef" label-position="right" label-width="120px" :model="form" :rules="formRules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager">
        <el-option v-for="item in list" :key="item.id" :label="item.username" :value="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" />
    </el-form-item>
    <el-form-item>
      <el-button @click="doclose">取消</el-button>
      <el-button type="primary" @click="doSubmit">确定</el-button>
    </el-form-item>
  </el-form>

</template>

<script>
import { getSimpleEmployee, addDepartments, getDepartmentById, editDepartments } from '@/api/departments'
export default {
  name: 'DepartmentDialog',
  props: {
    pid: { required: true, type: String },
    isEdit: { type: Boolean, required: true },
    originList: { type: Array, required: true }
  },
  data() {
    const validCode = (rule, value, callback) => {
      console.log('传过去的originList', this.originList)
      let codeList = this.originList.map(item => item.code)
      // 判断是否是编辑弹窗
      if (this.isEdit) {
        // 把自身code排除在外
        codeList = this.originList.filter(item => item.id !== this.pid).map(item => item.code)
      }
      if (codeList.includes(value)) {
        callback(new Error(value + '已被占用!'))
      } else {
        callback()
      }
    }
    const validName = (rule, value, callback) => {
      console.log('传过去的originList', this.originList)
      // 当前被点击的部门id是this.pid
      let nameList = this.originList.filter(item => item.pid === this.pid).map(item => item.name)
      // 如果是编辑部门名称
      if (this.isEdit) {
        const pid = this.originList.find(item => item.id === this.pid).pid
        // 查找与自己pid相同的兄弟部门&&把自己去掉
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.pid).map(item => item.name)
        // console.log( 'find...',this.originList.find(item=>item.id===this.pid));
        console.log('不能取的名字', nameList)
      }
      if (nameList.includes(value)) {
        callback(new Error(value + '已被占用!'))
      } else {
        callback()
      }
    }
    return {
      // 员工简单信息表
      list: [],
      // 表单值
      form: {
        name: '',
        manager: '',
        code: '',
        introduce: ''
      },
      // 表单校验规则
      formRules: {
        name: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '数据为1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '数据不能为空', trigger: 'change' },
          { min: 2, max: 50, message: '数据为1-50个字符', trigger: 'change' }
        ],
        code: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '数据为1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '数据为1-50个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 加载简单员工信息
    this.loadSimpleEmployee()
    // this.loadDepartmentById()
  },
  methods: {
    // 获取简单员工信息
    async loadSimpleEmployee() {
      const { data: res } = await getSimpleEmployee()
      // console.log('员工简单信息',res.data);
      this.list = res.data
    },
    // 增加部门
    async doSubmit() {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            console.log('form信息', this.form)
            const { data: res } = await editDepartments(this.form)
            console.log('编辑部门信息', res)
            // 子传父,编辑部门成功
            this.$emit('success')
          } else {
            // isEdit为false说明为新增框
            const { data: res } = await addDepartments({ ...this.form, pid: this.pid })
            console.log(res.data)
            // 子传父,添加部门成功
            this.$emit('success')
          }
        }
      })
    },
    //
    async loadDepartmentById() {
      if (this.isEdit) {
        // console.log(1111111111111);
        const { data: res } = await getDepartmentById(this.pid)
        // console.log('通过id查询部门信息', res);
        this.form = res.data
      }
    }, // 关闭弹窗
    doclose() {
      this.$emit('hclose')
    },
    // 重置表单
    restForm() {
      this.$refs.formRef.resetFields()
    }
  }
}
</script>

<style>

</style>
